<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /mon/default.php
# 厂家列表页面

# 载入常用集
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 4;

class page_index  extends page{
	public $sqlplus = "";
	public $qs = '';
	public $pagenumber = 0;
	public $pagearr = array();
	public $_audit = false; // $_audit = true; 仅显示 未完成审查的资金 $_audit = false; 为显示全部
	
	// 
	public $pageno = 1;
	public $audit = -1;
	public $payment = -1;
	public $way = -1;
	public $use = -1;
	public $dates_f;
	public $dates_l;

	function __construct(){
		global $config;
		global $_login;
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
		
		# 过滤数据
		# 页面
		if(isset($_GET['page'])){
			$this->pageno = $_GET['page'];
		}
		
		# 时间开始
		if(isset($_GET['dates_f'])){
			$this->dates_f = $_GET['dates_f'];
		}else{
			$this->dates_f = date("Y-m-1");
		}
		# 时间结束
		if(isset($_GET['dates_l'])){
			$this->dates_l = $_GET['dates_l'];
		}else{
			$this->dates_l = date("Y-m-d");
		}
		#between '2009-7-1' and '2009-7-7' 
		$this->sqlplus = $this->sqlplus." WHERE (cs2_money.dates between '".
			$this->dates_f ."' AND '".$this->dates_l."') ";	
		# 审查
		if(isset($_GET['audit'])){
			$this->audit = $_GET['audit'];
			if($this->audit != -1){
				$this->sqlplus = $this->sqlplus." AND (cs2_money.audit=".$this->audit.") ";
			}
		}
		# 收支
		if(isset($_GET['payment'])){
			$this->payment = $_GET['payment'];
			if($this->payment != -1){
				$this->sqlplus = $this->sqlplus." AND (cs2_money.payment=".$this->payment.") ";
			}
		}
		# 银行/现金/支票
		if(isset($_GET['way'])){
			$this->way = $_GET['way'];
			if($this->way != -1){
				$this->sqlplus = $this->sqlplus." AND (cs2_money.way=".$this->way.") ";
			}
		}
		# 用途
		if(isset($_GET['use'])){
			$this->use = $_GET['use'];
			if($this->use != -1){
				$this->sqlplus = $this->sqlplus." AND (cs2_money.use=".$this->use.") ";
			}
		}
	
		
		$this->qs = '&audit='.$this->audit.'&payment='.$this->payment.'&way='.$this->way.
			'&use='.$this->use.'&dates_f='.$this->dates_f.'&dates_l='.$this->dates_l;
		
		if($_login->checker == 1 || $_login->adminId == 1 || $_login->levelno > 79){
			
		}else{
			$this->sqlplus = $this->sqlplus." AND (cs2_money.adminId=" . $_login->adminId.")";
		}
		
		$config->smarty->assign('page', $this->pageno);
		$config->smarty->assign('qs', $this->qs);
	}
	function len(){
		global $config;
		$l = 0;
		$sql = "SELECT count(*) AS cnt FROM cs2_money ";
		$sql = $sql.$this->sqlplus.";";
		$row = $this->database->get_one($sql);
		$l = $row['cnt'];
		
		$this->pagenumber = $l / config::conf_web_pagecount;
	
		$config->smarty->assign('pagenumber', $this->pagenumber);
		
		for($i=1; $i<$this->pagenumber + 1; $i++){
			array_push($this->pagearr,$i);
		}
		$config->smarty->assign('pagearr', $this->pagearr);
			
		return $l;
	}
	function bank_listview(){
		# important
		$lv;
		try{
			$sql = "SELECT bankId,bank, branch, bankno, name
				FROM cs2_bank ORDER BY bankId ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			$lv = null;
		}
		return $lv;
	}
	function account_sum($paymet){
		$i = 0;
		try{
			$sql = "SELECT sum(cs2_money.account) as account FROM cs2_money ";
			$sql = $sql.$this->sqlplus;
			$sql = $sql." AND  (payment = ".$paymet.") ;";
			$row = $this->database->get_one($sql);
			$i = $row['account'];
			if($i == '')
				$i = 0;
		}catch (Exception $e){
			
		}
		return $i;
	}
	
	function listview(){
		$lv = null;
		try{
			$sql = "SELECT cs2_money.monId,cs2_money.account, cs2_money.payment, cs2_money.way, 
				cs2_money.use, cs2_money.adminId, cs2_money.audit,cs2_money.auditId, cs2_money.dates,cs2_money.info,
				cs2_bank.bank,cs2_bank.branch,cs2_bank.bankno,cs2_bank.name,cs2_admin1.name AS admin_name ,
				cs2_admin2.name As auditName
				FROM cs2_money LEFT OUTER JOIN (cs2_bank,cs2_admin AS cs2_admin1,cs2_admin AS cs2_admin2)
				ON (cs2_bank.bankId=cs2_money.way) AND 
			(cs2_admin1.adminId = cs2_money.adminId AND cs2_admin2.adminId = cs2_money.auditId)";
			
			$sql = $sql.$this->sqlplus;
			$sql = $sql." ORDER BY dates ASC LIMIT ".
				($this->pageno-1) * config::conf_web_pagecount.",".config::conf_web_pagecount.";";
			$lv = $this->database->select($sql);
		}catch (Exception $e){
			
		}
		return $lv;
	}
	function ajax_callback(){
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		if($token == config::TOKEN_ADD_MON){
			$dates = $_POST['dates'];
			$account = $_POST['account'];
			$payment = $_POST['payment'];
			$way = $_POST['way'];
			$use = $_POST['use'];
			$adminId = $_POST['adminId'];
			$info = $_POST['info'];
			$sql = "INSERT INTO cs2_money (dates, `account`, payment, `way`, `use`, adminId, audit,auditId,info) 
			VALUES ('".$dates."',".$account.",".$payment.",".$way.",".$use.",".$adminId.",0,".$adminId.",'".$info."');";
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_UP_MON){
			$dates = $_POST['dates'];
			$account = $_POST['account'];
			$payment = $_POST['payment'];
			$way = $_POST['way'];
			$use = $_POST['use'];
			$adminId = $_POST['adminId'];
			$monId = $_POST['monId'];
			$info = $_POST['info'];
			
			$sql = "UPDATE cs2_money SET 
				audit=0,auditId=".$adminId.",dates='".$dates."',`account`=".$account.",payment=".$payment.",
				way=".$way.",`use`=".$use.",adminId=".$adminId.",info='".$info."' WHERE monId=".$monId.";";
			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
			
		}
		if($token == config::TOKEN_AUDIT_MON){
			if (isset($_POST["PHPSESSID"])) {
				session_id($_POST["PHPSESSID"]);
			}
			$monId = $_POST['monId'];
			$audit = $_POST['audit'];
			$auditId = $_POST['auditId'];
			$sql = "UPDATE cs2_money SET audit=".$audit.",auditId=".$auditId." WHERE monId=".$monId.";";
			try {
				$this->database->update($sql);
				if($audit == 1){
					$json = array("ref"=>"OK");
				}else if($audit == 2){
					$json = array("ref"=>"NO");
				}
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		return json_encode($json);
	}
}

$pg = new page_index();

# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('pagecount', config::conf_web_pagecount);
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('listview', $pg->listview());
	$config->smarty->assign('account_sumin',$pg->account_sum(0));
	$config->smarty->assign('account_sumout',$pg->account_sum(1));
	$config->smarty->assign('money_dates_f',$pg->dates_f);
	$config->smarty->assign('money_dates_l',$pg->dates_l);
	$config->smarty->assign('bank_listview', $pg->bank_listview());
	$config->smarty->assign('use_array', $config->money_use_array);
	$config->smarty->assign('DATEFIRSTDAY',date("Y-m-1"));
	$config->smarty->assign('DATENOW',date("Y-m-d"));
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}
?>






